package com.prawn.ift.ctrlequipment.service.impl;

import com.prawn.ift.ctrlequipment.dao.CtrlequipmentStatusDao;
import com.prawn.ift.ctrlequipment.entity.CtrlEquipmentStatusVo;
import com.prawn.ift.ctrlequipment.pojo.Ctrlequipment;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentStatus;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentStatusService;
import entity.Result;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备状态管理服务实现
 */
@Service
public class CtrlEquipmentStatusServiceImpl implements CtrlEquipmentStatusService {

//    @Autowired

    @Autowired
    private CtrlequipmentStatusDao ctrlEquipmentStatusDao;

    /**
     * 添加控制状态设备信息
     *
     * @param ctrlEquipmentStatus
     * @return Result
     */
    @Override
    public Result addCtrlEquipmentStatus(CtrlequipmentStatus ctrlEquipmentStatus) {
        if (ctrlEquipmentStatusDao.save(ctrlEquipmentStatus) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除控制状态设备信息
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteCtrlEquipmentStatus(String id) {
        ctrlEquipmentStatusDao.deleteById(id);
        return Result.success();

    }

    /**
     * 修改控制状态设备信息
     *
     * @param ctrlEquipmentStatus
     * @return Result
     */
    @Override
    public Result editCtrlEquipmentStatus(CtrlequipmentStatus ctrlEquipmentStatus) {
        CtrlequipmentStatus oldCtrlEquipmentStatus =ctrlEquipmentStatusDao.findById(ctrlEquipmentStatus.getId()).get();
        BeanUtils.copyProperties(ctrlEquipmentStatus,oldCtrlEquipmentStatus, NullAttributeUtils.getNullPropertyNames(ctrlEquipmentStatus));



      ctrlEquipmentStatusDao.save(oldCtrlEquipmentStatus);
            return Result.success();

    }

    /**
     * 查询控制设备状态信息
     *
     * @param id
     * @return CtrlequipmentStatus
     */
    @Override
    public CtrlequipmentStatus getCtrlEquipmentStatusById(String id) {
        return ctrlEquipmentStatusDao.findById(id).get();
    }

    /**
     * 查询控制设备状态信息列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentStatusList() {
        List<CtrlequipmentStatus> ctrlEquipmentStatusList = ctrlEquipmentStatusDao.findAll();
        return Result.success().add(ctrlEquipmentStatusList);
    }

    /**
     * 多条件查询控制设备状态信息列表
     *
     * @param ctrlEquipmentStatusVo
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentStatusListByVo(CtrlEquipmentStatusVo ctrlEquipmentStatusVo) {



        List<CtrlequipmentStatus> ctrlEquipmentStatusList = ctrlEquipmentStatusDao.findAll(
                createSpecificationOfCtrlEquipmentStatusVo(ctrlEquipmentStatusVo));
        return Result.success().add(ctrlEquipmentStatusList);
    }

    /**
     * 分页查询控制设备状态信息列表
     *
     * @param page
     * @param rows
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentStatusPage(int page, int rows) {
        PageRequest pageRequest=PageRequest.of(page-1,rows);

        Page<CtrlequipmentStatus> pagelist = ctrlEquipmentStatusDao.findAll(pageRequest);
        Result result = new Result();
        result.setData(pagelist.getContent());
        return result;
    }


    /**
     * 动态条件构建
     * @param ctrlEquipmentStatusVo
     * @return
     */
    private Specification<CtrlequipmentStatus> createSpecificationOfCtrlEquipmentStatusVo(CtrlEquipmentStatusVo ctrlEquipmentStatusVo) {

        return new Specification<CtrlequipmentStatus>() {

            @Override
            public Predicate toPredicate(Root<CtrlequipmentStatus> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
              

                if (ctrlEquipmentStatusVo.getCtrlEquipmentId()!=null && !"".equals(ctrlEquipmentStatusVo.getCtrlEquipmentId())) {
                    predicateList.add(cb.equal(root.get("ctrlEquipmentId").as(String.class), ctrlEquipmentStatusVo.getCtrlEquipmentId()));
                }
                if (ctrlEquipmentStatusVo.getSceneId()!=null && !"".equals(ctrlEquipmentStatusVo.getSceneId())) {
                    predicateList.add(cb.equal(root.get("sceneId").as(String.class), ctrlEquipmentStatusVo.getSceneId()));
                }
                if (ctrlEquipmentStatusVo.getCtrlEquipmentStatus()!=null && !"".equals(ctrlEquipmentStatusVo.getCtrlEquipmentStatus())) {
                    predicateList.add(cb.equal(root.get("ctrlEquipmentStatus").as(String.class), ctrlEquipmentStatusVo.getCtrlEquipmentStatus()));
                }
                if (ctrlEquipmentStatusVo.getCreateDate()!=null && !"".equals(ctrlEquipmentStatusVo.getCreateDate())) {
                    predicateList.add(cb.equal(root.get("createDate").as(String.class), ctrlEquipmentStatusVo.getCreateDate()));
                }

                if (ctrlEquipmentStatusVo.getDelFlag()!=null && !"".equals(ctrlEquipmentStatusVo.getDelFlag())) {
                    predicateList.add(cb.equal(root.get("delFlag").as(String.class), ctrlEquipmentStatusVo.getDelFlag()));
                }


                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }



}
